<HTML>
<BODY BGCOLOR="white">
<PRE>
<FONT color="green">001</FONT>    package org.hackystat.iw.projectoverviewer;<a name="line.1"></a>
<FONT color="green">002</FONT>    <a name="line.2"></a>
<FONT color="green">003</FONT>    import java.io.FileInputStream;<a name="line.3"></a>
<FONT color="green">004</FONT>    import java.io.IOException;<a name="line.4"></a>
<FONT color="green">005</FONT>    import java.util.Map;<a name="line.5"></a>
<FONT color="green">006</FONT>    import java.util.Properties;<a name="line.6"></a>
<FONT color="green">007</FONT>    <a name="line.7"></a>
<FONT color="green">008</FONT>    /**<a name="line.8"></a>
<FONT color="green">009</FONT>     * Provides access to the values stored in the sensorbase.properties file. <a name="line.9"></a>
<FONT color="green">010</FONT>     * @author Philip Johnson<a name="line.10"></a>
<FONT color="green">011</FONT>     * @author Jianfei Liao<a name="line.11"></a>
<FONT color="green">012</FONT>     */<a name="line.12"></a>
<FONT color="green">013</FONT>    public class ProjectOverviewerProperties {<a name="line.13"></a>
<FONT color="green">014</FONT>      <a name="line.14"></a>
<FONT color="green">015</FONT>      /** The sensorbase host.  */<a name="line.15"></a>
<FONT color="green">016</FONT>      public static final String SENSORBASE_HOST_KEY =  "projectoverviewer.sensorbase.host";<a name="line.16"></a>
<FONT color="green">017</FONT>      /** The logging level.  */<a name="line.17"></a>
<FONT color="green">018</FONT>      public static final String LOGGING_LEVEL_KEY =  "projectoverviewer.logging.level";<a name="line.18"></a>
<FONT color="green">019</FONT>      /** The admin email key. */<a name="line.19"></a>
<FONT color="green">020</FONT>      public static final String ADMIN_EMAIL_KEY =  "projectoverviewer.admin.email";<a name="line.20"></a>
<FONT color="green">021</FONT>      /** How to control wicket development vs. deployment mode. */<a name="line.21"></a>
<FONT color="green">022</FONT>      public static final String WICKET_CONFIGURATION_KEY =  "projectoverviewer.wicket.configuration";<a name="line.22"></a>
<FONT color="green">023</FONT>      /** Optional name. */<a name="line.23"></a>
<FONT color="green">024</FONT>      public static final String APPLICATION_NAME_KEY =  "projectoverviewer.application.name";<a name="line.24"></a>
<FONT color="green">025</FONT>      <a name="line.25"></a>
<FONT color="green">026</FONT>      // Not sure yet if we need the following.<a name="line.26"></a>
<FONT color="green">027</FONT>      /** The projectoverviewer hostname key. */<a name="line.27"></a>
<FONT color="green">028</FONT>      //public static final String HOSTNAME_KEY = "projectoverviewer.hostname";<a name="line.28"></a>
<FONT color="green">029</FONT>      /** The projectoverviewer context root. */<a name="line.29"></a>
<FONT color="green">030</FONT>      //public static final String CONTEXT_ROOT_KEY = "projectoverviewer.context.root";<a name="line.30"></a>
<FONT color="green">031</FONT>      /** The projectoverviewer port key. */<a name="line.31"></a>
<FONT color="green">032</FONT>      //public static final String PORT_KEY = "projectoverviewer.port";<a name="line.32"></a>
<FONT color="green">033</FONT>      /** The SMTP host key. */<a name="line.33"></a>
<FONT color="green">034</FONT>      //public static final String SMTP_HOST_KEY =  "projectoverviewer.smtp.host";<a name="line.34"></a>
<FONT color="green">035</FONT>      /** The test install host key. */<a name="line.35"></a>
<FONT color="green">036</FONT>      //public static final String TEST_INSTALL_KEY =  "projectoverviewer.test.install";<a name="line.36"></a>
<FONT color="green">037</FONT>      <a name="line.37"></a>
<FONT color="green">038</FONT>      <a name="line.38"></a>
<FONT color="green">039</FONT>      /** Where we store the properties. */<a name="line.39"></a>
<FONT color="green">040</FONT>      private Properties properties; <a name="line.40"></a>
<FONT color="green">041</FONT>      <a name="line.41"></a>
<FONT color="green">042</FONT>      /**<a name="line.42"></a>
<FONT color="green">043</FONT>       * Creates a new ProjectOverviewerProperties instance. <a name="line.43"></a>
<FONT color="green">044</FONT>       * Prints an error to the console if problems occur on loading.<a name="line.44"></a>
<FONT color="green">045</FONT>       * @param properties A properties instance containing values that override those in the <a name="line.45"></a>
<FONT color="green">046</FONT>       * properties file. Can be null if no properties are to be overridden.  <a name="line.46"></a>
<FONT color="green">047</FONT>       */<a name="line.47"></a>
<FONT color="green">048</FONT>      public ProjectOverviewerProperties(Properties properties) {<a name="line.48"></a>
<FONT color="green">049</FONT>        try {<a name="line.49"></a>
<FONT color="green">050</FONT>          initializeProperties(properties);<a name="line.50"></a>
<FONT color="green">051</FONT>        }<a name="line.51"></a>
<FONT color="green">052</FONT>        catch (Exception e) {<a name="line.52"></a>
<FONT color="green">053</FONT>          System.out.println("Error initializing projectoverviewer properties.");<a name="line.53"></a>
<FONT color="green">054</FONT>        }<a name="line.54"></a>
<FONT color="green">055</FONT>      }<a name="line.55"></a>
<FONT color="green">056</FONT>     <a name="line.56"></a>
<FONT color="green">057</FONT>    <a name="line.57"></a>
<FONT color="green">058</FONT>      /**<a name="line.58"></a>
<FONT color="green">059</FONT>       * Reads in the properties in ~/.hackystat/projectoverviewer/projectoverviewer.properties<a name="line.59"></a>
<FONT color="green">060</FONT>       * if this file exists, <a name="line.60"></a>
<FONT color="green">061</FONT>       * and provides default values for all properties not mentioned in this file.<a name="line.61"></a>
<FONT color="green">062</FONT>       * @param testProperties A properties instance containing values that override those in the <a name="line.62"></a>
<FONT color="green">063</FONT>       * @throws Exception if errors occur.<a name="line.63"></a>
<FONT color="green">064</FONT>       */<a name="line.64"></a>
<FONT color="green">065</FONT>      private void initializeProperties (Properties testProperties) throws Exception {<a name="line.65"></a>
<FONT color="green">066</FONT>        String userHome = System.getProperty("user.home");<a name="line.66"></a>
<FONT color="green">067</FONT>        String propFile = userHome + "/.hackystat/projectoverviewer/projectoverviewer.properties";<a name="line.67"></a>
<FONT color="green">068</FONT>        this.properties = new Properties();<a name="line.68"></a>
<FONT color="green">069</FONT>        // Set defaults.<a name="line.69"></a>
<FONT color="green">070</FONT>        properties.setProperty(SENSORBASE_HOST_KEY, "http://localhost:9876/sensorbase");<a name="line.70"></a>
<FONT color="green">071</FONT>        properties.setProperty(LOGGING_LEVEL_KEY, "INFO");<a name="line.71"></a>
<FONT color="green">072</FONT>        properties.setProperty(ADMIN_EMAIL_KEY, "johnson@hackystat.org");<a name="line.72"></a>
<FONT color="green">073</FONT>        properties.setProperty(APPLICATION_NAME_KEY, "Hackystat Project Overviewer");<a name="line.73"></a>
<FONT color="green">074</FONT>    <a name="line.74"></a>
<FONT color="green">075</FONT>        // Now read in the properties file, and override the defaults if supplied. <a name="line.75"></a>
<FONT color="green">076</FONT>        FileInputStream stream = null;<a name="line.76"></a>
<FONT color="green">077</FONT>        try {<a name="line.77"></a>
<FONT color="green">078</FONT>          stream = new FileInputStream(propFile);<a name="line.78"></a>
<FONT color="green">079</FONT>          properties.load(stream);<a name="line.79"></a>
<FONT color="green">080</FONT>          System.out.println("Loading ProjectOverviewer properties from: " + propFile);<a name="line.80"></a>
<FONT color="green">081</FONT>        }<a name="line.81"></a>
<FONT color="green">082</FONT>        catch (IOException e) {<a name="line.82"></a>
<FONT color="green">083</FONT>          System.out.println(propFile + " not found. Using default properties.");<a name="line.83"></a>
<FONT color="green">084</FONT>        }<a name="line.84"></a>
<FONT color="green">085</FONT>        finally {<a name="line.85"></a>
<FONT color="green">086</FONT>          if (stream != null) {<a name="line.86"></a>
<FONT color="green">087</FONT>            stream.close();<a name="line.87"></a>
<FONT color="green">088</FONT>          }<a name="line.88"></a>
<FONT color="green">089</FONT>        }<a name="line.89"></a>
<FONT color="green">090</FONT>        // If we have supplied some properties as an argument, use these to override whatever we've <a name="line.90"></a>
<FONT color="green">091</FONT>        // done so far. <a name="line.91"></a>
<FONT color="green">092</FONT>        if (testProperties != null) {<a name="line.92"></a>
<FONT color="green">093</FONT>          this.properties.putAll(testProperties);<a name="line.93"></a>
<FONT color="green">094</FONT>        }<a name="line.94"></a>
<FONT color="green">095</FONT>        trimProperties(properties);<a name="line.95"></a>
<FONT color="green">096</FONT>        String wicketConfig = properties.getProperty(WICKET_CONFIGURATION_KEY);<a name="line.96"></a>
<FONT color="green">097</FONT>        if (wicketConfig != null) {<a name="line.97"></a>
<FONT color="green">098</FONT>          System.out.println("Setting wicket.configuration to: " + wicketConfig);<a name="line.98"></a>
<FONT color="green">099</FONT>          System.setProperty("wicket.configuration", wicketConfig);<a name="line.99"></a>
<FONT color="green">100</FONT>        }<a name="line.100"></a>
<FONT color="green">101</FONT>        <a name="line.101"></a>
<FONT color="green">102</FONT>        // I don't think we need to do the following for ProjectOverviewer, since we aren't using <a name="line.102"></a>
<FONT color="green">103</FONT>        // Mailer. Now add to System properties. Since the Mailer class expects to find this stuff <a name="line.103"></a>
<FONT color="green">104</FONT>        // on the System Properties, we will add everything to it.  In general, however, clients <a name="line.104"></a>
<FONT color="green">105</FONT>        // should not use the System Properties to get at these values, since that precludes running<a name="line.105"></a>
<FONT color="green">106</FONT>        // several SensorBases in a single JVM. And just is generally bogus. <a name="line.106"></a>
<FONT color="green">107</FONT>        //Properties systemProperties = System.getProperties();<a name="line.107"></a>
<FONT color="green">108</FONT>        //systemProperties.putAll(properties);<a name="line.108"></a>
<FONT color="green">109</FONT>        //System.setProperties(systemProperties);<a name="line.109"></a>
<FONT color="green">110</FONT>      }<a name="line.110"></a>
<FONT color="green">111</FONT>      <a name="line.111"></a>
<FONT color="green">112</FONT>      /**<a name="line.112"></a>
<FONT color="green">113</FONT>       * Returns a string containing the current properties.<a name="line.113"></a>
<FONT color="green">114</FONT>       * @return A string with the properties.  <a name="line.114"></a>
<FONT color="green">115</FONT>       */<a name="line.115"></a>
<FONT color="green">116</FONT>      public String echoProperties() {<a name="line.116"></a>
<FONT color="green">117</FONT>        String cr = System.getProperty("line.separator"); <a name="line.117"></a>
<FONT color="green">118</FONT>        String eq = " = ";<a name="line.118"></a>
<FONT color="green">119</FONT>        String pad = "                ";<a name="line.119"></a>
<FONT color="green">120</FONT>        return "ProjectOverviewer Properties:" + cr +<a name="line.120"></a>
<FONT color="green">121</FONT>          pad + SENSORBASE_HOST_KEY      + eq + get(SENSORBASE_HOST_KEY) + cr +<a name="line.121"></a>
<FONT color="green">122</FONT>          pad + LOGGING_LEVEL_KEY   + eq + get(LOGGING_LEVEL_KEY) + cr +<a name="line.122"></a>
<FONT color="green">123</FONT>          pad + WICKET_CONFIGURATION_KEY   + eq + get(WICKET_CONFIGURATION_KEY) + cr +<a name="line.123"></a>
<FONT color="green">124</FONT>          pad + APPLICATION_NAME_KEY   + eq + get(APPLICATION_NAME_KEY) + cr +<a name="line.124"></a>
<FONT color="green">125</FONT>          pad + ADMIN_EMAIL_KEY   + eq + get(ADMIN_EMAIL_KEY);<a name="line.125"></a>
<FONT color="green">126</FONT>      }<a name="line.126"></a>
<FONT color="green">127</FONT>      <a name="line.127"></a>
<FONT color="green">128</FONT>      /**<a name="line.128"></a>
<FONT color="green">129</FONT>       * Returns the value of the ProjectOverviewer Property specified by the key.<a name="line.129"></a>
<FONT color="green">130</FONT>       * @param key Should be one of the public static final strings in this class.<a name="line.130"></a>
<FONT color="green">131</FONT>       * @return The value of the key, or null if not found.<a name="line.131"></a>
<FONT color="green">132</FONT>       */<a name="line.132"></a>
<FONT color="green">133</FONT>      public String get(String key) {<a name="line.133"></a>
<FONT color="green">134</FONT>        return this.properties.getProperty(key);<a name="line.134"></a>
<FONT color="green">135</FONT>      }<a name="line.135"></a>
<FONT color="green">136</FONT>      <a name="line.136"></a>
<FONT color="green">137</FONT>      /**<a name="line.137"></a>
<FONT color="green">138</FONT>       * Ensures that the there is no leading or trailing whit20.space in the property values.<a name="line.138"></a>
<FONT color="green">139</FONT>       * The fact that we need to do this indicates a bug in Java's Properties implementation to me. <a name="line.139"></a>
<FONT color="green">140</FONT>       * @param properties The properties. <a name="line.140"></a>
<FONT color="green">141</FONT>       */<a name="line.141"></a>
<FONT color="green">142</FONT>      private void trimProperties(Properties properties) {<a name="line.142"></a>
<FONT color="green">143</FONT>        // Have to do this iteration in a Java 5 compatible manner. no stringPropertyNames().<a name="line.143"></a>
<FONT color="green">144</FONT>        for (Map.Entry&lt;Object, Object&gt; entry : properties.entrySet()) {<a name="line.144"></a>
<FONT color="green">145</FONT>          String propName = (String)entry.getKey();<a name="line.145"></a>
<FONT color="green">146</FONT>          properties.setProperty(propName, properties.getProperty(propName).trim());<a name="line.146"></a>
<FONT color="green">147</FONT>        }<a name="line.147"></a>
<FONT color="green">148</FONT>      }<a name="line.148"></a>
<FONT color="green">149</FONT>    }<a name="line.149"></a>




























































</PRE>
</BODY>
</HTML>
